package com.lzw.mall.order.util;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.HashMap;
import java.util.Map;

public class SendMsgUtil {
    public static void sendMsg(String tel,String code) throws Exception {
        Client client = createClient();
        SendSmsRequest sendSmsRequest = new SendSmsRequest();
        sendSmsRequest.setPhoneNumbers(tel);
        sendSmsRequest.setTemplateCode("SMS_275370167");
        sendSmsRequest.setSignName("lzw");
        Map<String,String> map = new HashMap<>();
        map.put("code", code);
        sendSmsRequest.setTemplateParam(new ObjectMapper().writeValueAsString(map));

            try {
                SendSmsResponse responce=client.sendSms(sendSmsRequest);
                System.out.println(responce);
            }catch (Exception e) {
                e.printStackTrace();
                System.out.println("发送短信失败");
            }
    }

    public static Client createClient() throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId("LTAI5tM9yr5P5166eemSkEtw")
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret("P3ioFDUcMUGEnRGyZlEACuIhJ4GYbd");
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }
}
